<template>
	<view>
		<view class="item" v-for="(res, index) in siteList" :key="res.id" v-if="dataBuffer" @click="goOrder(index)">
			<u-icon name="close-circle-fill" :size="40" color="#ff5500" :index="res.id"></u-icon>
			<view class="top">
				<view class="name">{{ res.username }}</view>
				<view class="phone">{{ res.userphone }}</view>
				<view class="tag">
					<text class="red" v-if="res.status == 1">默认</text>
				</view>
			</view>
			<view class="bottom">
				{{res.location+res.useraddress}}
				<u-icon name="edit-pen" :size="40" color="#999999" :index="res.id"
					@click.native.stop='goAddress(res.id)'></u-icon>
			</view>
		</view>
		<view v-else>
			暂无收货地址信息!
		</view>
		<view class="addSite" @tap="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				siteList: [], //初始化地址数据
				dataBuffer: false, //是否有地址
			};
		},
		async onShow() {
			let {
				uid,
				token
			} = uni.getStorageSync('userInfo');
			// 请求地址列表
			let result = await this.$http({
				url: '/addresslist',
				data: {
					uid,
				},
				header: {
					authorization: token
				}
			})
			// 请求成功
			if (result.data.code == 200) {
				this.siteList = result.data.list;
				this.dataBuffer = true
			} else {
				this.dataBuffer = false
			}
		},
		// 返回
		onBackPress(options) {
			// console.log(options);
			// 自定义返回
			if (options.from == 'backbutton') {
				// 跳转订单页面
				uni.navigateTo({
					url: '/pages/order/order'
				})
				return true
			} else {
				return false
			}

		},
		methods: {
			// 跳转到新增地址页面
			toAddSite() {
				uni.navigateTo({
					url: './addSite'
				})
			},
			// 跳转到修改地址
			goAddress(id) {
				// console.log(id);
				uni.navigateTo({
					url: './addSite?id=' + id
				})
			},
			//  选中地址  跳转订单页面
			goOrder(index) {
				// console.log(this.siteList[index]);
				let address = JSON.stringify(this.siteList[index])
				uni.navigateTo({
					url: '/pages/order/order?address=' + address
				})

			}

		}
	};
</script>

<style lang="scss" scoped>
	// 引入样式
	// @import '@/static/css/address.scss';
	.item {
		padding: 40rpx 20rpx;

		.top {
			display: flex;
			font-weight: bold;
			font-size: 34rpx;

			.phone {
				margin-left: 60rpx;
			}

			.tag {
				display: flex;
				font-weight: normal;
				align-items: center;

				text {
					display: block;
					width: 60rpx;
					height: 34rpx;
					line-height: 34rpx;
					color: #ffffff;
					font-size: 20rpx;
					border-radius: 6rpx;
					text-align: center;
					margin-left: 30rpx;
					background-color: rgb(49, 145, 253);
				}

				.red {
					background-color: red
				}
			}
		}

		.bottom {
			display: flex;
			margin-top: 20rpx;
			font-size: 28rpx;
			justify-content: space-between;
			color: #999999;
		}
	}

	.addSite {
		display: flex;
		justify-content: space-around;
		width: 600rpx;
		line-height: 100rpx;
		position: absolute;
		bottom: 30rpx;
		left: 80rpx;
		background-color: red;
		border-radius: 60rpx;
		font-size: 30rpx;

		.add {
			display: flex;
			align-items: center;
			color: #ffffff;

			.icon {
				margin-right: 10rpx;
			}
		}
	}
</style>